<?php

namespace common\logics;

use Yii;
use yii\web\UploadedFile;
use yii\helpers\Inflector;
use yii\helpers\ArrayHelper;

/**
 * 导入信息校验模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FilterImportColumnModel extends \common\base\Model {

    /**
     * @var UploadedFile
     */
    public $value;

    /**
     * @var string
     */
    public $name;

    /**
     * @var string
     */
    private $_data = null;

    /**
     * @var string
     */
    private $_key = null;

    /**
     * @var string
     */
    private $_expressCompany = [];

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['value'], 'validateValue'],
            [['name'], 'validateName'],
        ];
    }

    /**
     * 附件
     * @return \common\models\Attachment|null
     */
    public function validateValue($attribute) {
        $this->value = $this->baseFormat($this->$attribute);
    }

    /**
     * 附件
     * @return \common\models\Attachment|null
     */
    public function validateName($attribute) {
        $name = $this->$attribute;
        $data = static::getOrderAttributes();
        $key = array_search($name, $data);
        if ($key) {
            $this->_key = $key;
            $method = 'filter' . ucfirst(Inflector::variablize($key));
            if ($this->hasMethod($method)) {
                $this->$method();
            }
            $this->_data = $this->value;
        } else {
            $this->_data = null;
            $this->addError($attribute, '字段信息不存在：');
        }
    }

    /**
     * 附件
     * @return \common\models\Attachment|null
     */
    public static function getOrderAttributes() {
        return [
            'id' => '订单ID',
            'express_id' => '快递公司',
            'express_code' => '快递单号',
            'express_time' => '快递时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getData() {
        return [
            $this->_key => $this->_data
        ];
    }

    public function baseFormat($value) {
        if (is_float($value)) {
            return strval($value);
        }
        if ($value === null) {
            return '';
        }
        return $value;
    }

    public function getExpressCompany() {
        if (empty($this->_expressCompany)) {
            $this->_expressCompany = ArrayHelper::map(\common\models\base\BaseExpress::find()->all(), 'id', 'name');
        }
        return $this->_expressCompany;
    }

    protected function filterId() {
        if (!$this->value) {
            return $this->addError('value', '订单ID不能为留空');
        } else {
            if (!is_numeric($this->value)) {
                return $this->addError('value', '订单ID必需为数字');
            }
        }
    }

    protected function filterExpressId() {
        if (!$this->value) {
            return $this->addError('value', $this->value . '快递公司不能留空');
        } else {
            $companys = $this->getExpressCompany();
            $key = array_search($this->value, $companys);
            if (!$key) {
                Yii::error([
                    $this->value,
                    $companys,
                    $key
                ], __METHOD__);
                return $this->addError('value', $this->value . '快递公司不存在范围内');
            }
            $this->value = $key;
        }
    }

    protected function filterExpressCode() {
        if (!$this->value) {
            return $this->addError('value', '快递单号不能留空');
        }
    }

    protected function filterExpressTime() {
        $timestrap = time();
        if ($this->value) {
            $timestrap = strtotime($this->value);
        }
        if (!$timestrap) {
            $timestrap = time();
        }
        $this->value = intval($timestrap);
    }

}
